const { add } = require("../../mtools/math")

// pages/computer/computer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      numbers:[],
      marks:[],
      calculation:"",
      result:"",
      mark:"",
      flag:false,
      dot : true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  deletes(){
    this.data.numbers = [];
    this.data.marks = [];
    this.data.mark = "";
    this.data.flag = true;
    this.data.dot = true;
    this.data.result = "";
  },
  clear(){
    this.deletes();
    this.setData({result:"",calculation:""});
  },
  del(){
    if (this.data.calculation.split("").pop() >= '0' && this.data.calculation.split("").pop() <= '9' || this.data.calculation.split("").pop() == ".") {
      this.setData({calculation: this.data.calculation.substring(0,this.data.calculation.length-1)});
    }else{
      this.data.marks.pop();
      this.data.flag = true;
      this.setData({calculation: this.data.calculation.substring(0,this.data.calculation.length-1),result:""});
    }
  },
  splits(){
    this.data.numbers.push(this.data.calculation.split(/['\+','\-','\x','\÷']/).pop());
    this.maths();
    this.data.marks.push(this.data.mark);
    this.data.dot = true;
    console.log(this.data.numbers);
    console.log(this.data.marks);
  },

  add(){
    if(this.data.flag){
      this.data.flag = !this.data.flag;
      this.data.mark = "+";
      this.splits();
     
      this.setData({calculation:this.data.calculation+"+",result:this.data.result});
    }
   
  },
  reduce(){
    if(this.data.flag){
      this.data.flag = !this.data.flag;
      this.data.mark = "-";
      this.splits();
      this.setData({calculation:this.data.calculation+'-',result:this.data.result});
    }
  },
  mul(){
    if(this.data.flag){
      this.data.flag = !this.data.flag;
      this.data.mark = "*";
      this.splits();
      this.setData({calculation:this.data.calculation+'x',result:this.data.result});
    }
  },
  divi(){
    if(this.data.flag){
      this.data.flag = !this.data.flag;
      this.data.mark = "÷";
      this.splits();
      this.setData({calculation:this.data.calculation+'÷',result:this.data.result});
    }
  },
  num(e){
    if (e.target.dataset.num == '.') {
      if (this.data.dot) {
        this.setData({calculation:this.data.calculation+e.target.dataset.num});
        this.data.dot = false;
        this.data.flag = true;
      }
    }else{
      this.setData({calculation:this.data.calculation+e.target.dataset.num});
      this.data.flag = true;
    }
  },
  equal(){
    if(this.data.flag){
      this.data.numbers.push(this.data.calculation.split(/[\'+','\-','\x','\÷']/).pop());
      console.log(this.data.numbers);
      console.log(this.data.marks);
      this.maths();
      this.setData({result:"",calculation:this.data.result+""});
      this.deletes();
    }
  },
  maths(){
    let ml = this.data.marks.length;
    this.data.result = Number(this.data.numbers[0]);
    for(let i = 0;i<ml;i++){
      this.data.numbers[i+1] = Number(this.data.numbers[i+1]); 
      switch(this.data.marks[i]){
        case '+':{
          this.data.result = this.data.result+this.data.numbers[i+1];
          break;
        };
        case '-':{
          this.data.result = this.data.result-this.data.numbers[i+1];
          break;
        };
        case '*':{
          this.data.result = this.data.result*this.data.numbers[i+1];
          break;
        };
        case '÷':{
          this.data.result = this.data.result/this.data.numbers[i+1];
          break;
        };
        default:{
          break;
        }
      }
      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})